package cn.xabad.commons.tools;

import cn.xabad.commons.lang3.StringUtils;

// +----------------------------------------------------------------------
// | CreateTime: 15/11/26 
// +----------------------------------------------------------------------
// | Author:     xab(http://www.xueyong.net.cn)
// +----------------------------------------------------------------------
// | CopyRight:  http://www.boxfish.cn
// +----------------------------------------------------------------------
public class StringU extends StringUtils {

    /**
     * 判断是否含有UTF-16表情
     * @param text
     * @return
     */
    public static String stripEmoji(String text) {
        StringBuilder builder = new StringBuilder();
        long count = Character.codePointCount(text, 0, text.length());
        for (int index = 0; index < count; index++) {
            int codePoint = Character.codePointAt(text, index);
            if (Character.charCount(codePoint) > 1) {
                builder.appendCodePoint(codePoint);
            }
        }
        return builder.toString();
    }

    /**
     * 中文算2字节，英文为1
     * @param value
     * @return
     */
    public static int length(String value) {
        if (StringU.isEmpty(value)) return 0;
        int valueLength = 0;
        String chinese = "[\u0391-\uFFE5]";
        /* 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1 */
        for (int i = 0; i < value.length(); i++) {
            /* 获取一个字符 */
            String temp = value.substring(i, i + 1);
            /* 判断是否为中文字符 */
            if (temp.matches(chinese)) {
                /* 中文字符长度为2 */
                valueLength += 2;
            } else {
                /* 其他字符长度为1 */
                valueLength += 1;
            }
        }
        return valueLength;
    }
}
